import request from '@/utils/request'

// 药品相关接口
export const medicineApi = {
  // 获取分类列表
  getCategories() {
    return request({
      url: '/api/medicine/categories',
      method: 'GET'
    })
  },

  // 获取药品列表
  getMedicineList(params) {
    console.log('API接收到的参数:', JSON.stringify(params))
    return request({
      url: '/api/medicine/list',
      method: 'POST',
      data: params
    }).then(res => {
      if (res.code === 200) {
        console.log(`API成功返回数据，共 ${res.data?.records?.length || 0} 条`)
      } else {
        console.error('API返回错误:', res)
      }
      return res
    }).catch(err => {
      console.error('API请求失败:', err)
      throw err
    })
  },

  // 获取药品详情
  getMedicineDetail(id) {
    return request({
      url: `/api/medicine/detail/${id}`,
      method: 'GET'
    })
  },

  // 获取药品库存
  getMedicineStock(medicineId) {
    return request({
      url: `/api/medicine/stock/${medicineId}`,
      method: 'GET'
    })
  },

  // 更新药品库存
  updateMedicineStock(data) {
    return request({
      url: '/api/medicine/stock/update',
      method: 'POST',
      data
    })
  },

  // 获取药品评价列表
  getMedicineEvaluations({ medicineId, pageNum = 1, pageSize = 10 }) {
    console.log('获取评价列表参数:', { medicineId, pageNum, pageSize });
    return request({
      url: `/api/medicine/evaluations/${medicineId}`,
      method: 'GET',
      params: {
        pageNum,
        pageSize
      }
    }).then(res => {
      console.log('评价列表响应:', res);
      return res;
    }).catch(err => {
      console.error('获取评价列表失败:', err);
      throw err;
    });
  },

  // 搜索药品
  searchMedicines(params) {
    return request({
      url: '/medicine/search',
      method: 'GET',
      params
    })
  },

  // 获取用户未评价的订单
  getUserOrders(params) {
    return request({
      url: '/api/medicine/user-orders',
      method: 'GET',
      params
    })
  },

  // 添加药品评价
  addMedicineEvaluation(data) {
    return request({
      url: '/api/medicine/evaluation/evaluation',
      method: 'POST',
      data
    })
  },

  // 点赞/取消点赞
  toggleEvaluationLike(evaluationId) {
    return request({
      url: `/api/medicine/evaluation/evaluation/like/${evaluationId}`,
      method: 'POST'
    })
  },

  // 添加评价回复
  addEvaluationReply(data) {
    return request({
      url: '/api/medicine/evaluation/evaluation/reply',
      method: 'POST',
      data
    })
  },

  // 获取评价回复列表
  getEvaluationReplies(evaluationId) {
    return request({
      url: `/api/medicine/evaluation/evaluation/replies/${evaluationId}`,
      method: 'GET'
    })
  },

  // 举报评价
  reportEvaluation(data) {
    return request({
      url: '/api/medicine/evaluation/report',
      method: 'POST',
      data
    })
  }
} 